package com.atbun.ucenterservice.service.impl;

import com.atbun.commonutils.JwtUtils;
import com.atbun.commonutils.MD5;
import com.atbun.commonutils.ResultObj;
import com.atbun.servicebase.exceptionHandler.GuliException;
import com.atbun.ucenterservice.entity.UcenterMember;
import com.atbun.ucenterservice.entity.vo.RegisterVo;
import com.atbun.ucenterservice.mapper.UcenterMemberMapper;
import com.atbun.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-15
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    /**
     * 用户登录
     * @param ucenterMember
     * @return
     */
    @Override
    public String login(UcenterMember ucenterMember) {

//        手机号 密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
//        校验用手机号与密码是否存在
        if (StringUtils.isEmpty(mobile) ||
            StringUtils.isEmpty(password)){
            throw new GuliException(20001,"必填项不能为空");
        }

//        获取会员
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
//        查出来的对象
        UcenterMember ucenterMemberOnPhone = this.baseMapper.selectOne(queryWrapper);
        if (null == ucenterMemberOnPhone){
            throw new GuliException(20001,"请注册该账号");
        }

//        校验密码
        if(!MD5.encrypt(password).equals(ucenterMemberOnPhone.getPassword())) {
            throw new GuliException(20001,"密码错误");
        }

        //校验是否被禁用
        if(ucenterMemberOnPhone.getIsDisabled()) {
            throw new GuliException(20001,"该账户被冻结，请联系管理员");
        }
    //   使用JWT生成token字符串
        String token = JwtUtils.getJwtToken(ucenterMemberOnPhone.getId(), ucenterMemberOnPhone.getNickname());
        return token;
    }

    /**
     * 会员注册
     * @param registerVo
     */
    @Override
    public boolean register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String nickname = registerVo.getNickname(); // 昵称
        String mobile = registerVo.getMobile(); // 手机号
        String password = registerVo.getPassword(); // 密码
        String code = registerVo.getCode(); // 验证码

        //校验参数
        if(StringUtils.isEmpty(nickname) ||
                StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new GuliException(20001,"必填项不能为空");
        }

//        查询数据库中是否存在相同的手机号码。
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer count = this.baseMapper.selectCount(queryWrapper);
        if (count.intValue() > 0){  // 查询到的信息大于0，说明有信息
            throw new GuliException(20002,"该账号已注册，请登录");
        }

//        校验验证码，从redis获取发送的验证码
        String mobileCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(mobileCode)){
            throw new GuliException(20001,"验证码失效或检查验证码是否正确");
        }

//        添加注册信息到数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setNickname(nickname);
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password)); //密码需要加密后加入数据库
        ucenterMember.setIsDeleted(false); // 是否删除，false未删除==0，true已删除==1
//      用户默认头像
        ucenterMember.setAvatar("https://edu-10086.oss-cn-shenzhen.aliyuncs.com/2020/05/0588d4a1942b7d4afda14b11ed0083d24dfile.png");
        boolean save = this.save(ucenterMember);
        return save;
    }

    /**
     * 根据扫码后的openid查询用户信息
     * @param openid
     * @return
     */
    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        UcenterMember ucenterMember = this.baseMapper.selectOne(queryWrapper);
        return ucenterMember;
    }

    /**
     * 统计每一天的注册人数
     * @param day
     * @return
     */
    @Override
    public Integer countRegister(String day) {
        Integer count = this.baseMapper.countRegister(day);
        return count;
    }
}
